<template>

  <view class="app-container">
    <view>
		<u-toast ref="uToast" />
	</view>
    <video ref="localVideo"  page_gesture=true crossOrigin="anonymous" page-gesture='true' controls="controls" style="width:100%;height:100%" autoplay></video>

    <!-- <button @click="gua" class="gua">挂断</button> -->
    <view class="gua"  @click="gua">
      <img src="@/static/guaduan.png" style="width: 100rpx;height: 100rpx;" alt="">
    </view>
  </view>
</template>

<script>
import GLOBAL_VARIABLE from "../helmet/global_variable.js";

export default {
  data() {
    return {
      sipsession: {},
      roomid: ""
    };
  },
  onLoad: function(val) {
    // uni.showLoading({
    //   title: "加载中",
    //   mask: true
    // });

    this.g_id = val.g_id;
    this.admin_id = val.admin_id;
    this.host = val.host;
    // console.log(this.host)
    this.user = val.user;
    this.tfgroup = val.tfgroup; // 配置通话参数
    this.roomid = val.roomid;
    // console.log(this.tfgroup)
    if (this.tfgroup) {
      this.group();
    } else {
      this.single();
    }
  },
  // onBackPress(options) {
  //   console.log("要指定返回的页面");
  //   this.sipsession.terminate();
  //   return true;
  // },
  methods: {
    single() {
      let _this = this;
      const user = _this.user;
      const host = _this.host;
      const uri = user + "@" + host;
      const options = {
        sessionDescriptionHandlerOptions: {
          constraints: {
            audio: true,
            video: true //或直接写“video:true”，若选择语音通话则为“video:false”
          }
        }
      };

      // 拨打电话
      let outgoingSession = GLOBAL_VARIABLE.HOST.invite(uri, options); //“1100200@47.112.134.21:5060”为被拨打者账号
      _this.sipsession = outgoingSession;
      outgoingSession.on("accepted", data => {
        var pc = outgoingSession.sessionDescriptionHandler.peerConnection;

        var remoteStream = new MediaStream();
        pc.getReceivers().forEach(function(receiver) {
          remoteStream.addTrack(receiver.track);
          window.rs = remoteStream;
          _this.nav = navigator.userAgent;
          document.getElementsByTagName("video")[0].srcObject = rs;
        });
      });
      outgoingSession.on("rejected", (response, cause) => {
        console.log("拒绝 ", response, cause);
        this.$refs.uToast.show({
          title: "请求被拒绝！",
          type: "error"
        });
        _this.sipsession.terminate();
        uni.navigateTo({
          url:
            "/pages/helmet/toukui?g_id=" +
            this.g_id +
            "&admin_id=" +
            this.admin_id +
            "&host=" +
            this.host +
            "&trueone=" +
            true
        });
      });

      outgoingSession.on("failed", (response, cause) => {
        console.log("失败 ", response, cause);
        this.$refs.uToast.show({
          title: "请求失败！",
          type: "error"
        });
        _this.sipsession.terminate();
        uni.navigateTo({
          url:
            "/pages/helmet/toukui?g_id=" +
            this.g_id +
            "&admin_id=" +
            this.admin_id +
            "&host=" +
            this.host +
            "&trueone=" +
            true
        });
      });

      outgoingSession.on("terminated", (message, cause) => {
        console.log("请求结束 - ", message, cause);
        uni.hideLoading();
        this.$refs.uToast.show({
          title: "请重新尝试连接！",
          type: "error"
        });
        _this.sipsession.terminate();
        uni.navigateTo({
          url:
            "/pages/helmet/toukui?g_id=" +
            this.g_id +
            "&admin_id=" +
            this.admin_id +
            "&host=" +
            this.host +
            "&trueone=" +
            true
        });
      });
      uni.hideLoading();
    },
    group() {
      let _this = this;
      let options = {
        sessionDescriptionHandlerOptions: {
          constraints: {
            audio: true,
            video: true //或可加入一些配置项{faceMode:’user’,width:160,height:120,frameRate:16,}，群语音通话设为false
          }
        }
      };
      let outgoingSession = GLOBAL_VARIABLE.HOST.invite(
        this.roomid + "@" + this.host,
        options
      );
      _this.currentSession = outgoingSession;
      outgoingSession.on("accepted", data => {
        var pc = outgoingSession.sessionDescriptionHandler.peerConnection;

        var remoteStream = new MediaStream();
        pc.getReceivers().forEach(function(receiver) {
          remoteStream.addTrack(receiver.track);
          window.rs = remoteStream;
          _this.nav = navigator.userAgent;
          document.getElementsByTagName("video")[0].srcObject = rs;
        });
      });
      outgoingSession.on("rejected", (response, cause) => {
        console.log("拒绝 ", response, cause);
        this.$refs.uToast.show({
          title: "请求被拒绝！",
          type: "error"
        });
        _this.currentSession.terminate();
        uni.navigateTo({
          url:
            "/pages/helmet/toukui?g_id=" +
            this.g_id +
            "&admin_id=" +
            this.admin_id +
            "&host=" +
            this.host +
            "&trueone=" +
            true
        });
      });

      outgoingSession.on("failed", (response, cause) => {
        console.log("失败 ", response, cause);
        this.$refs.uToast.show({
          title: "请求失败！",
          type: "error"
        });
        _this.currentSession.terminate();
        uni.navigateTo({
          url:
            "/pages/helmet/toukui?g_id=" +
            this.g_id +
            "&admin_id=" +
            this.admin_id +
            "&host=" +
            this.host +
            "&trueone=" +
            true
        });
      });

      outgoingSession.on("terminated", (message, cause) => {
        console.log("请求结束 - ", message, cause);
        uni.hideLoading();
        this.$refs.uToast.show({
          title: "请重新尝试连接！",
          type: "error"
        });
        _this.currentSession.terminate();
        uni.navigateTo({
          url:
            "/pages/helmet/toukui?g_id=" +
            this.g_id +
            "&admin_id=" +
            this.admin_id +
            "&host=" +
            this.host +
            "&trueone=" +
            true
        });
      });
      uni.hideLoading();
    },
    gua() {
      if (this.tfgroup) {
        console.log('群组挂断')
        this.currentSession.terminate();
         uni.navigateTo({
          url:
            "/pages/helmet/toukui?g_id=" +this.g_id +
            "&admin_id=" +this.admin_id +
            "&host=" +this.host
        });
      } else {
        this.sipsession.terminate();
        uni.navigateTo({
          url:
            "/pages/helmet/toukui?g_id=" +this.g_id +
            "&admin_id=" +this.admin_id +
            "&host=" +this.host
        });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.app-container {
  height: 99%;
  position: relative;
}
.gua {
  position: absolute;
  bottom: 70rpx;
  width: 100rpx;
  height: 100rpx;
  overflow: hidden;
  margin-left: 45%;
}
</style>
